//
//  ImageViewStyle.swift
//  EasyCoding
//
//  Created by Fanxx on 2019/7/15.
//

import UIKit

extension UCSetting where TargetType: UIImageView {
    ///图片
    public static func image(_ image:UIImage?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.image = image
        })
    }
    ///图片
    public static func image(_ named:String) -> UCSetting<TargetType> {
        return image(UIImage(named: named))
    }
    ///图片
    public static func image(_ named:String, resizable insets: UIEdgeInsets) -> UCSetting<TargetType> {
        return image(UIImage(named: named)?.resizableImage(withCapInsets: insets, resizingMode: .stretch))
    }
    ///图片
    public static func highlighted(image:UIImage?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.highlightedImage = image
        })
    }
    ///图片
    public static func highlighted(image named:String) -> UCSetting<TargetType> {
        return highlighted(image:UIImage(named: named))
    }
    ///图片变形
    public static func resizable(_ insets:UIEdgeInsets) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.image = target.image?.resizableImage(withCapInsets: insets)
            target.contentMode = .scaleToFill
        })
    }
    ///图片变形
    public static func resizable(_ top: CGFloat, _ left: CGFloat,_ bottom: CGFloat, _ right: CGFloat) -> UCSetting<TargetType> {
        return resizable(UIEdgeInsets(top: top, left: left, bottom: bottom, right: right))
    }
    
    ///动画图片
    public static func animation(_ images:[UIImage]?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.animationImages = images
        })
    }
    ///动画图片
    public static func highlighted(animation images:[UIImage]?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.highlightedAnimationImages = images
        })
    }
    ///动画时长
    public static func animation(duration: TimeInterval) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.animationDuration = duration
        })
    }
    ///动画重复次数, 0代表无限，默认0
    public static func animation(repeat count: Int) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.animationRepeatCount = count
        })
    }
    ///启动动画
    public static func animating() -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.startAnimating()
        })
    }
}
